//
//  XDPickerCityView.m
//  kabu
//
//  Created by XiangDongMBP on 2017/3/6.
//  Copyright © 2017年 East. All rights reserved.
//

#import "XDPickerCityView.h"
#import "UIColor+XDHex.h"

@interface XDPickerCityView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray<NSDictionary *> *cityArray;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *completeBtn;

@end

static CGFloat const kBottomViewHeight = 230.0;
static CGFloat const kBtnHeight = 40.0;

@implementation XDPickerCityView

- (instancetype)initWithTitles:(NSArray *)titles delegate:(id)delegate {
    self = [self init];
    if (self) {
        _titles = titles;
        _delegate = delegate;
//        [self initSubViews];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    self.alpha = 0;
    
    CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
    CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
//    CGFloat height = screenHeight * 0.4;
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, screenWidth, kBottomViewHeight)];
    bottomView.backgroundColor = [UIColor whiteColor];
    self.bottomView = bottomView;
    [self addSubview:bottomView];
    
    [self.bottomView addSubview:self.cancelBtn];
    [self.bottomView addSubview:self.completeBtn];
    [self.bottomView addSubview:self.pickerView];
    
}

- (void)showInView:(UIView *)view {
    if (self.superview) {
        return;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
//    CGRect viewFrame = [view convertRect:view.bounds toView:nil];
//    __block CGRect frame = self.tableView.frame;
//    frame.origin.x = CGRectGetMinX(viewFrame) + (CGRectGetWidth(viewFrame) - CGRectGetWidth(frame)) / 2.0 ;
//    frame.origin.y = CGRectGetMaxY(viewFrame);
//    self.tableView.frame = frame;
//    
//    // 选中某一个cell
//    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    [UIView animateWithDuration:0.2 animations:^{
        CGRect frame = self.bottomView.frame;
        frame.origin.y = CGRectGetHeight([UIScreen mainScreen].bounds) - kBottomViewHeight;
        self.bottomView.frame = frame;
        self.alpha = 1;
    } completion:^(BOOL finished) {
        ;
    }];
}

- (void)hideWithAnimation:(BOOL)animate {
    CGFloat duration = animate ? 0.2 : 0.0;
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:duration animations:^{
        CGRect frame = self.bottomView.frame;
        frame.origin.y = CGRectGetHeight([UIScreen mainScreen].bounds);
        self.bottomView.frame = frame;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)dismiss {
    [self hideWithAnimation:YES];
}

//- (void)buttonTapped:(UIButton *)button {
//    if ([self.delegate respondsToSelector:@selector(eTChSeaSelView:clickedButtonAtIndex:)]) {
//        [self.delegate eTChSeaSelView:self clickedButtonAtIndex:button.tag];
//    }
//    [self hideWithAnimation:YES];
//}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//
//    //    for (UIButton * button in self.buttonArray) {
//    //        if ([button pointInside:point withEvent:event]) {
//    //            return button;
//    //        }
//    //    }
//
//    UIView * hitView = [super hitTest:point withEvent:event];
//    if ([hitView isKindOfClass:[UIButton class]]) {
//        return hitView;
//    }
//
//    [self hideWithAnimation:YES];
//    return nil;
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self hideWithAnimation:YES];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.cityArray.count;
    }
    
    NSDictionary *dic = nil;
    NSInteger row_1 = [pickerView selectedRowInComponent:0];
    if (row_1 < self.cityArray.count) {
        dic = self.cityArray[row_1];
    }
    NSArray *value = dic.allValues.firstObject;
    return value.count;
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        NSDictionary *dic = self.cityArray[row];
        return dic.allKeys.firstObject;
    }
    
    NSInteger row_1 = [pickerView selectedRowInComponent:0];
    NSDictionary *dic = nil;
    if (row_1 < self.cityArray.count) {
        dic = self.cityArray[row_1];
    }
    NSArray *value = dic.allValues.firstObject;
    if (row < value.count) {
        return value[row];
    }
    return value.firstObject;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        [pickerView reloadComponent:1];
    }
}

#pragma mark - event response
- (void)cancelBtnTapped:(UIButton *)btn {
    [self hideWithAnimation:YES];
}
- (void)completeBtnTapped:(UIButton *)btn {
    NSInteger row_1 = [self.pickerView selectedRowInComponent:0];
    NSInteger row_2 = [self.pickerView selectedRowInComponent:1];
    
    NSDictionary *dic = nil;
    if (row_1 < self.cityArray.count) {
        dic = self.cityArray[row_1];
    }
    NSArray *cityArray = dic.allValues.firstObject;
    NSString *province = dic.allKeys.firstObject;
    NSString *city = (row_2 < cityArray.count) ? cityArray[row_2] : cityArray.firstObject;
    
    if ([self.delegate respondsToSelector:@selector(xDPickerCityView:province:city:)]) {
        [self.delegate xDPickerCityView:self province:province city:city];
    }
    
    [self hideWithAnimation:YES];
}

#pragma mark - setters and getters 
- (NSArray<NSDictionary *> *)cityArray {
    if (!_cityArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cityData" ofType:@"plist"];
        _cityArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _cityArray;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.frame = CGRectMake(0, 0, 50, kBtnHeight);
//        _cancelBtn.backgroundColor = [UIColor yellowColor];
        [_cancelBtn addTarget:self action:@selector(cancelBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
- (UIButton *)completeBtn {
    if (!_completeBtn) {
        _completeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_completeBtn setTitle:@"完成" forState:UIControlStateNormal];
        CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds);
        _completeBtn.frame = CGRectMake(width - 50, 0, 50, kBtnHeight);
//        _completeBtn.backgroundColor = [UIColor yellowColor];
        [_completeBtn addTarget:self action:@selector(completeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _completeBtn;
}
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kBtnHeight, CGRectGetWidth([UIScreen mainScreen].bounds), kBottomViewHeight - kBtnHeight)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.backgroundColor = [UIColor xd_colorWithHex:0xF3F3F3];
    }
    return _pickerView;
}

@end
